<?php

class Product extends CI_Controller {

	public function Product()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->model('Productmodel');
        $this->load->model('Usermodel');
        $this->load->library('session');
	}
	
	public function index()
	{
            
			$username = $this->session->userdata('username');
            if($username){
                $this->load->model('Usermodel');
                $userId = $this->Usermodel->getUserIdByLogin($username);
                $this->load->model('Productmodel');
                $viewData['products'] = $this->Productmodel->getAllProducts($userId);
                $viewData['username'] = $username;
                $this->load->view('product_list_view', $viewData);
            }
            else{
                $this->load->view('access_denined_view', null);
            }
	}
	
       

        public function Create(){
		$username = $this->session->userdata('username');
            if(!$username){
                $this->load->view('access_denined_view', null);
                return;
            }
            $this->load->view('create_product_ajax_view', null);
        }
		

        public function Save(){
		$username = $this->session->userdata('username');
            if(!$username){
                echo("Access denined");
            }
            else{ 		
				$productCode = $_POST['ProductCode'];
				$name = $_POST['Name'];
				$shortInfo = $_POST['ShortInfo'];
				
			$array = array();
			}
			
			if(preg_match_all('/[A-Za-z!@#$%^&*\(\)\[\]\'\";:.,<>\?]+/', $productCode, $array) > 0){
				echo 'Incorrectly entered Product Code';
			} else if(preg_match_all('/[!@#$%^&*\(\)=+;:,.\'\"]+/', $name, $array) > 0 || $name == ""){
				echo 'Incorrectly entered name';
			} else if(preg_match_all('/[@#^&*+=?]+/', $shortInfo, $array) > 0 ){
				echo 'Incorrectly entered shortInfo';
			} else {
								
					$user_id = $this->Usermodel->getUserIdByLogin($username); 
                
					$this->Productmodel->addProduct($productCode, $name, $shortInfo, $user_id);
					echo 'success';	
			}
		}
        		
		public function Delete(){
		$username = $this->session->userdata('username');
            if(!$username){
                $this->load->view('access_denined_view', null);
            }
			else{
				$this->load->view('delete_product_ajax_view', null);
			}
		}
		
		public function DeleteButtonClick(){
			$username = $this->session->userdata('username');
            if(!$username){
                $this->load->view('access_denined_view', null);
            }
			else{
				$productCode = $_POST['ProductCode'];
				if(preg_match_all('/[A-Za-z!@#$%^&*\(\)\[\]\'\";:.,<>\?]+/', $productCode, $array) > 0){
					echo 'Incorrectly entered Product Code';
				} else{
					$this->Productmodel->deleteProduct($productCode);				
					echo 'success';
				}
			}
		}
		
		public function Edit(){
			$username = $this->session->userdata('username');
            if(!$username){
                $this->load->view('access_denined_view', null);
                return;
            }
            $this->load->view('edit_product_ajax_view', null);
		}
		
		public function EditButtonClick(){
			$username = $this->session->userdata('username');
            if(!$username){
                echo("Access denined");
            }
            else{ 		
				$productCode = $_POST['ProductCode'];
				$name = $_POST['Name'];
				$shortInfo = $_POST['ShortInfo'];
            
				if(preg_match_all('/[A-Za-z!@#$%^&*\(\)\[\]\'\";:.,<>\?]+/', $productCode, $array) > 0){
					echo 'Incorrectly entered Product Code';
				} else if(preg_match_all('/[!@#$%^&*\(\)=+;:,.\'\"]+/', $name, $array) > 0){
					echo 'Incorrectly entered name';
				} else if(preg_match_all('/[@#^&*+=?]+/', $shortInfo, $array) > 0 ){
					echo 'Incorrectly entered shortInfo';
				} else {
			
					$this->Productmodel->editProduct($productCode, $name, $shortInfo);
					echo 'success';		
				}				
			}
			
		}
	
	// Function for simulator
	public function simGetAllProducts() {
		if (!isset($_POST['login']) ||
            !isset($_POST['password'])) {
            return;
        }
		
		$username = $_POST['login'];
		$password = $_POST['password'];
		
		if (!$this->Usermodel->loginPassValid($username, $password)) {
            return;
        }
		$userId = $this->Usermodel->getUserIdByLogin($username);	
		$products = $this->Productmodel->getAllProducts($userId);
		
		foreach ($products as &$product) {
			echo "@".$product['ProductCode']."@".$product['Name']."@".$product['ShortInfo']."@";
		}
	}
}
?>